package service

import (
	"learning/gooop/spring/demo/order/dto"
	"learning/gooop/spring/demo/order/entity"
	"sync"
	"time"
)

// @Service
type MockOrderService struct {
	headers map[int]*entity.OrderHeadEntity
	items   map[int]*entity.OrderItemEntity
	rwmutex *sync.RWMutex
}

func NewMockOrderService() IOrderService {
	it := new(MockOrderService)
	it.init()
	return it
}

func (me *MockOrderService) init() {
	me.headers = make(map[int]*entity.OrderHeadEntity)
	me.items = make(map[int]*entity.OrderItemEntity)
	me.rwmutex = new(sync.RWMutex)
}

func (me *MockOrderService) Save(head *entity.OrderHeadEntity, items []*entity.OrderItemEntity) error {
	me.rwmutex.Lock()
	defer me.rwmutex.Unlock()

	me.headers[head.OrderID] = head
	for _, it := range items {
		me.items[it.ItemID] = it
	}

	return nil
}

func (me *MockOrderService) Get(orderID int) (error, *dto.OrderDTO) {
	now := time.Now()
	return nil, &dto.OrderDTO{
		Header: &dto.OrderHeadDTO{
			OrderHeadEntity: entity.OrderHeadEntity{
				OrderID:           orderID,
				OrderNO:           "order-001",
				OrderDate:         &now,
				CustomerID:        1,
				DeliveryAddressID: 1,
				StatusFlag:        1,
				CreatorID:         2,
				CreateTime:        &now,
				UpdatorID:         3,
				UpdateTime:        &now,
			},
			CustomerName:    "customer-name",
			DeliveryAddress: "delivery-address",
			CreatorName:     "creator-name",
			UpdatorName:     "updator-name",
			StatusName:      "status-name",
		},

		Items: []*dto.OrderItemDTO{
			{
				OrderItemEntity: entity.OrderItemEntity{
					ItemID:     1,
					OrderID:    orderID,
					ProductID:  1,
					Quantity:   100,
					Price:      66,
					Amount:     6600,
					UpdateTime: &now,
				},
				ProductName:  "product-name",
				ProductSpecs: "product-specs",
			},
		},
	}
}

func (me *MockOrderService) Query(customerID int, statusFlag int, dateFrom string, dateTo string, pageNO int, pageSize int) (error, []*dto.OrderHeadDTO) {
	now := time.Now()
	return nil, []*dto.OrderHeadDTO{
		{
			OrderHeadEntity: entity.OrderHeadEntity{
				OrderID:           1,
				OrderNO:           "order-001",
				OrderDate:         &now,
				CustomerID:        1,
				DeliveryAddressID: 1,
				StatusFlag:        1,
				CreatorID:         2,
				CreateTime:        &now,
				UpdatorID:         3,
				UpdateTime:        &now,
			},
			CustomerName:    "customer-name",
			DeliveryAddress: "delivery-address",
			CreatorName:     "creator-name",
			UpdatorName:     "updator-name",
			StatusName:      "status-name",
		},

		{
			OrderHeadEntity: entity.OrderHeadEntity{
				OrderID:           2,
				OrderNO:           "order-002",
				OrderDate:         &now,
				CustomerID:        2,
				DeliveryAddressID: 2,
				StatusFlag:        2,
				CreatorID:         3,
				CreateTime:        &now,
				UpdatorID:         4,
				UpdateTime:        &now,
			},
			CustomerName:    "customer-name-2",
			DeliveryAddress: "delivery-address-2",
			CreatorName:     "creator-name-3",
			UpdatorName:     "updator-name-4",
			StatusName:      "status-name-2",
		},
	}
}
